﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Management;

namespace client
{
    class UniqueId
    {

        /// <summary>
        /// Vai buscar o Digital Product id
        /// </summary>
        /// <returns>string</returns>
        private string GetDigitalProductId()
        {
            RegistryKey key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
                                          RegistryView.Registry64);

            string keyPath = @"Software\Microsoft\Windows NT\CurrentVersion";
            byte[] rpk = (byte[])key.OpenSubKey(keyPath).GetValue("DigitalProductId");

            return Convert.ToBase64String(rpk);
        }

        /// <summary>
        /// vai buscar o serial number do disco rigido
        /// </summary>
        /// <returns>string</returns>
        private string GetHardDiskSerial() // nao esquecer de adicionar a referencia System.management
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");

            string serial = "";
            foreach (ManagementObject wmi_HD in searcher.Get())
            {
                // get the hardware serial no.
                if (wmi_HD["SerialNumber"] != null)
                    serial = wmi_HD["SerialNumber"].ToString();
            }
            serial.Trim();
            return serial;
        }

        /// <summary>
        /// Vai buscar o serial number da Bios
        /// </summary>
        /// <returns>string</returns>
        private string GetBiosSerial()
        {
            ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT SerialNumber FROM Win32_BaseBoard");
            ManagementObjectCollection moc = mos.Get();

            string serial = "";

            foreach (ManagementObject mo in moc)
            {
                serial = mo["SerialNumber"].ToString();
            }

            return serial;
        }

        /// <summary>
        /// Gera um numero aleatorio
        /// </summary>
        /// <returns>int</returns>
        private int GenerateID()
        {
            Random rand = new Random();
            int res = rand.Next(000000000, 999999999);

            return res;
        }

        /// <summary>
        /// Devolve o unico id com o digital product id, serial number do disco rigido, serial number da bios e um numero gerado aleatoriamente
        /// </summary>
        /// <returns>string</returns>
        public string GetUniqueID()
        {
            return "#1#" + this.GetDigitalProductId() + "#2#" + this.GetHardDiskSerial().TrimStart().TrimEnd() + 
                "#3#" + this.GetBiosSerial() + "#4#" + this.GenerateID().ToString();
        }

        public string GetUniqueID(string serial)
        {
            return "#1#" + this.GetDigitalProductId() + "#2#" + this.GetHardDiskSerial().TrimStart().TrimEnd() + 
                "#3#" + this.GetBiosSerial() + "#4#" + serial;
        }


        /// <summary>
        /// Reparte o Unique Id que contem todos os ids
        /// </summary>
        /// <param name="str"></param>
        /// <returns>utilizador</returns>
        public Utilizador partirString(String str)
        {
            try
            {
                List<string> lista = str.Split('#').ToList<string>();

                Utilizador util = new Utilizador();
                int i = 0;

                foreach (var item in lista)
                {
                    if (item.Trim() == "1")
                        util.ProdutoID = lista[i + 1].ToString();

                    if (item.Trim() == "2")
                        util.DiskSerial = lista[i + 1].ToString();

                    if (item.Trim() == "3")
                        util.SerialBios = lista[i + 1].ToString();

                    if (item.Trim() == "4")
                        util.SerialUnico = lista[i + 1].ToString();

                    i++;
                }


                return util;
            }
            catch (Exception ex)
            {
                Console.WriteLine("partirStringIds: " + ex.Message);
                return null;
            }
        }



    }
}
